package com.williambucciero.mobilemuseum;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.williambucciero.mobilemuseum.R;

public class InformationScreen extends Activity 
implements OnClickListener{

	public static String museumLink = null;
	private DebugReporter reporter;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        reporter = new DebugReporter(this);
        setContentView(R.layout.information_screen);
        init();
    }

    private void init(){		
    	Button museumButton = (Button)findViewById(R.id.museumLink);
    	
    	museumButton.setOnClickListener(this);
    	
    	JSONDownloaderTask task = new JSONDownloaderTask();
    	task.setView((TextView)findViewById(R.id.informationText));
    	
    	try{
    		task.execute(new URL("http://williambucciero.com/photoinfo/1.json"));
    	} catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	BackgroundAlarm ba = new BackgroundAlarm(this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.layout.information_screen_menu, menu);
    	return(true);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case R.id.send_bug:
    		Intent i = new Intent(Intent.ACTION_SEND);
    		i.setType("message/rfc822");
    		i.putExtra(Intent.EXTRA_EMAIL  , new String[]{getResources().getString(R.string.supportEmail)});
    		i.putExtra(Intent.EXTRA_SUBJECT, "Debug Report: " + Calendar.getInstance().get(Calendar.DAY_OF_YEAR));
    		i.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + CachedDebugFileProvider.AUTHORITY + "/debug.txt"));
    		try {
    		    startActivity(Intent.createChooser(i, getResources().getString(R.string.chooserSupportEmail)));
    		} catch (android.content.ActivityNotFoundException ex) {
    		    Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    		}
   		}
    	return true;
    }
    
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch(id){
		case R.id.museumLink:
			if(museumLink != null){
				Uri uri = Uri.parse(museumLink);
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			} else {
				Toast.makeText(this, this.getResources().getString(R.string.museumLinkNotFound), Toast.LENGTH_LONG).show();
			}
		}
    	finish();
	}
}

class JSONDownloaderTask extends AsyncTask<URL, Void, String> {
	private TextView view;
	
	public void setView(TextView in){
		view=in;
	}
    
    public String doInBackground(URL... url) {
    	String toReturn=null;
    	try {
    		Calendar c = Calendar.getInstance();
    		StartServiceOnBootReceiver.count = c.get(Calendar.DAY_OF_WEEK);

    		BufferedInputStream in = new BufferedInputStream(url[0].openStream());
    		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"),8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			in.close();
			JSONArray info = new JSONArray(sb.toString());
			JSONObject placardObject = (JSONObject)info.get(StartServiceOnBootReceiver.count-1);
			InformationScreen.museumLink = (String)placardObject.getString("museumLink");
			toReturn = (String)placardObject.get("placard");
    	} catch (Exception e) {
    		toReturn=null;
		    e.printStackTrace();
		}
		return toReturn;
	}
    
    @Override
    protected void onPostExecute(String text) {    
    	if(text!=null){
    		view.setText(text);
    	} else {
    		view.setText(R.string.infoNotFound);
    	}
    }
}
